//
//  oldEditAddressViewController.m
//  laonianbang
//
//  Created by 勇拓 李 on 18/3/12.
//  Copyright © 2018年 zaiyou. All rights reserved.
//

#import "oldEditAddressViewController.h"
#import "oldAddressChooseViewController.h"
#import "oldMyAddressesListViewController.h"
#import "oldMapLocationViewController.h"

@interface oldEditAddressViewController ()
@property (weak, nonatomic) IBOutlet UILabel *label_address;
@property (weak, nonatomic) IBOutlet UITextField *textfiled_doorNo;
@property (weak, nonatomic) IBOutlet UIButton *btn_save;
@property (weak, nonatomic) IBOutlet UIView *address_view;
@property (nonatomic, assign)BOOL isNew;
@end

@implementation oldEditAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self initData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - initView
- (void)initView
{
    
    self.navigationItem.title = @"编辑地址";
    if (_addressInfo) {
        _isNew = NO;
    }
    else
    {
        _isNew = YES;
    }
    [self initAddress];
    
    _btn_save.layer.cornerRadius = 6.f;
    [_btn_save addTarget:self action:@selector(btn_save_tapped:) forControlEvents:UIControlEventTouchDown];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gotoChooseAddress:)];
    [_address_view addGestureRecognizer:tap];
}

- (void)setAddressInfo:(oldAddressInfo *)addressInfo
{
    _addressInfo = addressInfo;
    [self initAddress];
}

- (void)initAddress
{
    if (_addressInfo) {
        _label_address.text = _addressInfo.address;
        _label_address.textColor = OLD_BLACK;
        _textfiled_doorNo.text = _addressInfo.doorNo;
    }
}

#pragma mark - initData
- (void)initData
{
    
}

#pragma mark - chooseAddress
- (void)gotoChooseAddress:(id)sender
{
    oldMapLocationViewController *vc = [oldMapLocationViewController new];
    vc.addressInfo = _addressInfo;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - btn
- (void)btn_save_tapped:(id)sender
{
    NSString *url = nil;
    
    if (!_isNew) {
        url = URL_EditAddress;
    }
    else
    {
        url = URL_AddAddress;
    }
    
    if (!_addressInfo.lat || !_addressInfo.lng || !_addressInfo.address) {
        [oldHub warningUser:@"请先选择地址"];
        return;
    }
    
    NSCharacterSet  *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    NSString *text = [_textfiled_doorNo.text stringByTrimmingCharactersInSet:set];
    
    if (![text length]) {
        [oldHub warningUser:@"门牌号不能为空"];
        return;
    }
    
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:_addressInfo.lat, @"lat", _addressInfo.lng, @"lng", _addressInfo.address, @"address", nil];
    
    if(!_isNew)
    {
        [params setObject:_addressInfo.aid forKey:@"aid"];
    }
    
    [params setObject:text forKey:@"doorNo"];
    
    WeakSelf
    
    [oldHub waiting:@"保存中.."];
    
    [OLDNetWork httpRequestWithPostPut:url params:params success:^(id data) {
        [oldHub hideHUD];
        UIViewController *vc = weakSelf.navigationController.viewControllers[[weakSelf.navigationController.viewControllers count] - 2];
        
        if ([vc isKindOfClass:[oldAddressChooseViewController class]]) {
            oldAddressChooseViewController *oldVc = (oldAddressChooseViewController *)vc;
            [oldVc needReload];
        }
        else if ([vc isKindOfClass:[oldMyAddressesListViewController class]])
        {
            oldMyAddressesListViewController *oldVc = (oldMyAddressesListViewController *)vc;
            [oldVc needReload];
        }
        
        [weakSelf.navigationController popViewControllerAnimated:YES];
    } error:^(NSString *error) {
        [oldHub warningUser:error];
    }];
}

@end
